<!DOCTYPE HTML>
<html>
<head>
    <head>
    <#include "../common/_meta.html">
        <title>修改用户信息</title>
    </head>
</head>
<body>
<article class="cl pd-20">
    <form class="form form-horizontal" id="from-update-user">
        <input type="text" style="display: none;" value="${user.id}" readonly id="id" name="id">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">用户名：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" style="background-color: #EEEEEE;" value="${user.name}" readonly id="name" name="name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>昵称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="${user.nickName}" placeholder="" id="nickName" name="nickName">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">等级：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <select id="level" name="level" class="select" <#if level?exists && level gt 0> style="width: 160px;background-color: #EEEEEE;" disabled="disabled"<#else>style="width: 160px;"</#if>>
                    <option value="1" name="level" <#if user.level==1> selected="true"</#if>>管理员</option>
                    <option value="2" name="level" <#if user.level==2> selected="true"</#if>>用户</option>
                </select>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">集群授权：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <dl class="permission-list">
                    <dt>
                        <label>
                            <input type="checkbox" onclick="selectAll()" value="全部" name="全部" id="selectAll">所有集群</label>
                    </dt>
                    <div id = "clusterList">
                    <#if clusters?exists>
                        <#list clusters?keys as key>
                            <dd>
                                <label>
                                    <input type="checkbox" name = "clusters"
                                           <#if clusters[key]=="1">checked="true"</#if> value="${key}">
                                    ${key}</label>
                            </dd>
                        </#list>
                    </#if>
                    </div>
                </dl>
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;保存&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>

<#include "../common/_footer.html">

<script type="text/javascript">
    window.onload = function () {
        var all = document.getElementById ("selectAll");
        var cl = document.getElementById ("clusterList");
        var ach = cl.getElementsByTagName ("input");

        //设置全选按钮选中的时候，遍历选中所有子复选框
        all.onclick = function () {
            for ( var i = 0; i < ach.length; i++) {
                ach[i].checked = this.checked;
            }
        }
        //子复选框有一个未选中时，去掉全选按钮的选中状态
        for ( var i = 0; i < ach.length; i++) {
            ach[i].onclick = function () {
                if ( !this.checked ) {
                    all.checked = false;
                }
            };
        }
    }

    $(function() {
        $("#from-update-user").validate({
            rules: {
                nickName: {
                    required: true
                },
            },
            onkeyup: false,
            focusCleanup: true,
            success: "valid",
            submitHandler: function () {
                invoke();
            }
        });
    });

    /* 修改用户信息 */
    function invoke() {
        var index = layer.load(2);
        $.ajax({
            type: "POST",
            url: "${base}/user/update",
            data: $('#from-update-user').serialize(),
            dataType: "json",
            success: function (result) {
                layer.close(index);
                if(result.code === 0) {
                    layer.msg("修改成功", {icon:6,time:1000},
                            function () {
                                layer_close();
                            }
                    );
                } else {
                    layer.alert("修改失败", {icon:5,time:1000},
                            function () {layer_close();}
                    );
                }
            },
            error: function (result) {
                layer.close(index);
                layer.msg("内部错误", {icon:5,time:1000},
                        function () {layer_close();}
                );
            }
        });
    }
</script>
</body>
</html>